package com.mes.spc.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
	public static void main(String[] args) { 
//		List<Map<String,Object>> dateMap = getThisMonthWeekDate(true);
//		for(Map<String,Object> map:dateMap){
//			for(String key:map.keySet()){
//				System.out.println(key+"--------------"+map.get(key));
//			}
//		}
		
//		getThisMonthWeekDate(true, "2018-06-02");
		
		
		
		String today = "2018-06-09";  
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		Date date = null;  
		try {  
		    date = format.parse(today);  
		} catch (ParseException e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}  
		  
		Calendar calendar = Calendar.getInstance();  
		calendar.setFirstDayOfWeek(Calendar.SATURDAY);  
		calendar.setTime(date);  
		  
		System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); 
		
	}
	
	/**
     * 获取本月周次和日期时间段信息
     * 
     * @return
     */
    public static List<Map<String,Object>> getThisMonthWeekDate(boolean isLog) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

        // 1 获取本月的第一天
        Date firstDayOfMonth = getFirstDayOfMonth();
        Calendar firstDayOfMonthCal = Calendar.getInstance();
        firstDayOfMonthCal.setFirstDayOfWeek(Calendar.MONDAY);
        firstDayOfMonthCal.setTime(firstDayOfMonth);

        // 2 获取本月第一周第一天
        Date firstWeekFirstDay = getFirstDayOfFirstWeekOfMonth(true);
        Calendar firstWeekFirstDayCal = Calendar.getInstance();
        firstWeekFirstDayCal.setFirstDayOfWeek(Calendar.MONDAY);
        firstWeekFirstDayCal.setTime(firstWeekFirstDay);

        // 3 获取本次周期起止时间
        Date sDate = null;// 本月第一周第一天
        Date eDate = null;// 本月最后一周最后一天
        Calendar sCal = Calendar.getInstance();
        Calendar eCal = Calendar.getInstance();
        sCal.setFirstDayOfWeek(Calendar.MONDAY);
        sCal.setFirstDayOfWeek(Calendar.MONDAY);

        // 获取本月第一天是周几
        int weekDay = firstDayOfMonthCal.get(Calendar.DAY_OF_WEEK) - 1;

        // 如果是周末，起始时间就是本月第一周第一天
        // 否则起始时间就是本月第一周第一天 倒退7天
        if (weekDay == 6 || weekDay == 0 || weekDay == 1) {// 周六或周天
            sDate = firstWeekFirstDay;
        } else {
            firstWeekFirstDayCal.getTime();
            firstWeekFirstDayCal.add(Calendar.DATE, -7);
            sDate = firstWeekFirstDayCal.getTime();
        }
        sCal.setTime(sDate);

        // 结束时间是起始时间+34天（5周）
        eCal.setTime(sDate);
        eDate = eCal.getTime();
        eCal.add(Calendar.DATE, 34);
        eDate = eCal.getTime();

        Date cDate = new Date();
        if (cDate.getTime() < eDate.getTime()) {
            eDate = cDate;
        }

        if (isLog) {
            System.out.println(" 本月统计起止时间是：" + format.format(sDate) + " 到 "
                    + format.format(eDate));
            System.out
                    .println("-------------------------------------------------------------------------------");
        }

        // 4 循环得到周次信息
        for (int i = 0; i < 5; i++) {// 循环5周次
        	Map<String,Object> model = new HashMap<String, Object>();
            Calendar endTime = Calendar.getInstance();
            endTime.setFirstDayOfWeek(Calendar.MONDAY);
            endTime.setTime(sDate);
            endTime.set(Calendar.HOUR_OF_DAY, 23);
            endTime.set(Calendar.MINUTE, 59);
            endTime.set(Calendar.SECOND, 59);
            endTime.add(Calendar.DATE, 6);

            model.put("indexOfWeek", (i + 1));
            model.put("startDateOfWeek", sDate);
            model.put("endDateOfWeek", endTime.getTime());

            Date currDate = new Date();
            // 如果当前时间在某个周次内
            if (sDate.getTime() < currDate.getTime()
                    && endTime.getTime().getTime() > currDate.getTime()) {
                model.put("endDateOfWeek", currDate);
                if (isLog) {
                    System.out.println(" 本月第" + (i + 1) + "周起止时间是："
                            + format.format(sDate) + " 到 "
                            + format.format(currDate));
                }
                list.add(model);
                break;
            }
            if (isLog) {
                System.out.println(" 本月第" + (i + 1) + "周起止时间是："
                        + format.format(sDate) + " 到 "
                        + format.format(endTime.getTime()));
            }
            list.add(model);

            sCal.add(Calendar.DATE, 7);
            sDate = sCal.getTime();
        }

        return list;
    }

    // 获取本月第一天
    public static Date getFirstDayOfMonth(Date date) {
        Calendar now = Calendar.getInstance();
//        Date date = new Date(2015 - 1900, 12 - 1, 1);
//        now.setTime(date);
        now.setTime(date);
        now.set(Calendar.DATE, now.getActualMinimum(Calendar.DATE));
        now.set(Calendar.HOUR_OF_DAY, 0);
        now.set(Calendar.MINUTE, 0);
        now.set(Calendar.SECOND, 0);
        return now.getTime();
    }

    // 获取本月第一周第一天
    public static Date getFirstDayOfFirstWeekOfMonth(boolean isLog,Date date) {
        Calendar now = Calendar.getInstance();
        now.setFirstDayOfWeek(Calendar.SATURDAY);
        now.setTime(getFirstDayOfMonth(date));

        int i = 1;
        while (now.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            now.set(Calendar.DAY_OF_MONTH, i++);// 设置这个月的星期1 为几号
        }

        Date firstMonday = now.getTime();// 取得日期和时间
        String dtStr = new SimpleDateFormat("YYYY-MM-dd").format(firstMonday);// 格式化日期
        if (isLog) {
            System.out
                    .println("-------------------------------------------------------------------------------");
            System.out.println(" 本月第一周第一天是：" + dtStr);
            System.out
                    .println("-------------------------------------------------------------------------------");
        }
        return now.getTime();
    }

    /**
     * 获取本周周次和日期时间段信息
     * 
     * @return
     */
    public static Map<String,Object> getThisWeekDate(Date date) {

    	Map<String,Object> model = new HashMap<String, Object>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 根据今天的时间获取本周属于本月的第几周
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 获取本周一的日期

        int wom = now.get(Calendar.WEEK_OF_MONTH);
//        model.setIndexOfWeek(wom);
        model.put("indexOfWeek", wom);

        // 根据今天的时间获取本周的开始时间
        now.set(Calendar.HOUR_OF_DAY, 0);
        now.set(Calendar.MINUTE, 0);
        now.set(Calendar.SECOND, 0);
        now.set(Calendar.MILLISECOND, 0);
        System.out.println("********" + now.get(Calendar.YEAR) + "年"
                + ((now.get(Calendar.MONTH)) + 1) + "月第" + wom + "周星期一的日期是："
                + df.format(now.getTime()));
//        model.setStartDateOfWeek(now.getTime());
        model.put("startDateOfWeek", now.getTime());

        // 根据今天的时间获取本周的结束时间
        now.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        now.add(Calendar.WEEK_OF_YEAR, 1);
        now.set(Calendar.HOUR_OF_DAY, 23);
        now.set(Calendar.MINUTE, 59);
        now.set(Calendar.SECOND, 59);
        now.set(Calendar.MILLISECOND, 999);
        System.out.println("********" + now.get(Calendar.YEAR) + "年"
                + ((now.get(Calendar.MONTH)) + 1) + "月第" + wom + "周星期天的日期是："
                + df.format(now.getTime()));
//        model.setEndDateOfWeek(now.getTime());
        model.put("endDateOfWeek", now.getTime());

        return model;
    }
    
    
    
    /**
     * 获取本月周次和日期时间段信息
     * 
     * @return
     */
    public static List<Map<String,Object>> getThisMonthWeekDate(boolean isLog,String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

        SimpleDateFormat dateShortFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date zdyDate = null;
		try {
			zdyDate = dateShortFormat.parse(dateStr);
		} catch (ParseException e) {
			zdyDate = new Date();
		}
        // 1 获取本月的第一天
        Date firstDayOfMonth = getFirstDayOfMonth(zdyDate);
        Calendar firstDayOfMonthCal = Calendar.getInstance();
        firstDayOfMonthCal.setFirstDayOfWeek(Calendar.MONDAY);
        firstDayOfMonthCal.setTime(firstDayOfMonth);

        // 2 获取本月第一周第一天
        Date firstWeekFirstDay = getFirstDayOfFirstWeekOfMonth(true,zdyDate);
        Calendar firstWeekFirstDayCal = Calendar.getInstance();
        firstWeekFirstDayCal.setFirstDayOfWeek(Calendar.MONDAY);
        firstWeekFirstDayCal.setTime(firstWeekFirstDay);

        // 3 获取本次周期起止时间
        Date sDate = null;// 本月第一周第一天
        Date eDate = null;// 本月最后一周最后一天
        Calendar sCal = Calendar.getInstance();
        Calendar eCal = Calendar.getInstance();
        sCal.setFirstDayOfWeek(Calendar.MONDAY);
        sCal.setFirstDayOfWeek(Calendar.MONDAY);

        // 获取本月第一天是周几
        int weekDay = firstDayOfMonthCal.get(Calendar.DAY_OF_WEEK) - 1;

        // 如果是周末，起始时间就是本月第一周第一天
        // 否则起始时间就是本月第一周第一天 倒退7天
        if (weekDay == 6 || weekDay == 0 || weekDay == 1) {// 周六或周天
            sDate = firstWeekFirstDay;
        } else {
            firstWeekFirstDayCal.getTime();
            firstWeekFirstDayCal.add(Calendar.DATE, -7);
            sDate = firstWeekFirstDayCal.getTime();
        }
        sCal.setTime(sDate);

        // 结束时间是起始时间+34天（5周）
        eCal.setTime(sDate);
        eDate = eCal.getTime();
        eCal.add(Calendar.DATE, 34);
        eDate = eCal.getTime();

        Date cDate = new Date();
        if (cDate.getTime() < eDate.getTime()) {
            eDate = cDate;
        }

        if (isLog) {
            System.out.println(" 本月统计起止时间是：" + format.format(sDate) + " 到 "
                    + format.format(eDate));
            System.out
                    .println("-------------------------------------------------------------------------------");
        }

        // 4 循环得到周次信息
        for (int i = 0; i < 5; i++) {// 循环5周次
        	Map<String,Object> model = new HashMap<String, Object>();
            Calendar endTime = Calendar.getInstance();
            endTime.setFirstDayOfWeek(Calendar.MONDAY);
            endTime.setTime(sDate);
            endTime.set(Calendar.HOUR_OF_DAY, 23);
            endTime.set(Calendar.MINUTE, 59);
            endTime.set(Calendar.SECOND, 59);
            endTime.add(Calendar.DATE, 6);

            model.put("indexOfWeek", (i + 1));
            model.put("startDateOfWeek", sDate);
            model.put("endDateOfWeek", endTime.getTime());

            Date currDate = new Date();
            // 如果当前时间在某个周次内
            if (sDate.getTime() < currDate.getTime()
                    && endTime.getTime().getTime() > currDate.getTime()) {
                model.put("endDateOfWeek", currDate);
                if (isLog) {
                    System.out.println(" 本月第" + (i + 1) + "周起止时间是："
                            + format.format(sDate) + " 到 "
                            + format.format(currDate));
                }
                list.add(model);
                break;
            }
            if (isLog) {
                System.out.println(" 本月第" + (i + 1) + "周起止时间是："
                        + format.format(sDate) + " 到 "
                        + format.format(endTime.getTime()));
            }
            list.add(model);

            sCal.add(Calendar.DATE, 7);
            sDate = sCal.getTime();
        }

        return list;
    }

    // 获取本月第一天
    public static Date getFirstDayOfMonth() {
        Calendar now = Calendar.getInstance();
//        Date date = new Date(2015 - 1900, 12 - 1, 1);
//        now.setTime(date);
        now.set(Calendar.DATE, now.getActualMinimum(Calendar.DATE));
        now.set(Calendar.HOUR_OF_DAY, 0);
        now.set(Calendar.MINUTE, 0);
        now.set(Calendar.SECOND, 0);
        return now.getTime();
    }

    // 获取本月第一周第一天
    public static Date getFirstDayOfFirstWeekOfMonth(boolean isLog) {
        Calendar now = Calendar.getInstance();
        now.setFirstDayOfWeek(Calendar.MONDAY);
        now.setTime(getFirstDayOfMonth());

        int i = 1;
        while (now.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            now.set(Calendar.DAY_OF_MONTH, i++);// 设置这个月的星期1 为几号
        }

        Date firstMonday = now.getTime();// 取得日期和时间
        String dtStr = new SimpleDateFormat("YYYY-MM-dd").format(firstMonday);// 格式化日期
        if (isLog) {
            System.out
                    .println("-------------------------------------------------------------------------------");
            System.out.println(" 本月第一周第一天是：" + dtStr);
            System.out
                    .println("-------------------------------------------------------------------------------");
        }
        return now.getTime();
    }

    /**
     * 获取本周周次和日期时间段信息
     * 
     * @return
     */
    public static Map<String,Object> getThisWeekDate() {

    	Map<String,Object> model = new HashMap<String, Object>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 根据今天的时间获取本周属于本月的第几周
        Calendar now = Calendar.getInstance();
        now.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 获取本周一的日期

        int wom = now.get(Calendar.WEEK_OF_MONTH);
//        model.setIndexOfWeek(wom);
        model.put("indexOfWeek", wom);

        // 根据今天的时间获取本周的开始时间
        now.set(Calendar.HOUR_OF_DAY, 0);
        now.set(Calendar.MINUTE, 0);
        now.set(Calendar.SECOND, 0);
        now.set(Calendar.MILLISECOND, 0);
        System.out.println("********" + now.get(Calendar.YEAR) + "年"
                + ((now.get(Calendar.MONTH)) + 1) + "月第" + wom + "周星期一的日期是："
                + df.format(now.getTime()));
//        model.setStartDateOfWeek(now.getTime());
        model.put("startDateOfWeek", now.getTime());

        // 根据今天的时间获取本周的结束时间
        now.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        now.add(Calendar.WEEK_OF_YEAR, 1);
        now.set(Calendar.HOUR_OF_DAY, 23);
        now.set(Calendar.MINUTE, 59);
        now.set(Calendar.SECOND, 59);
        now.set(Calendar.MILLISECOND, 999);
        System.out.println("********" + now.get(Calendar.YEAR) + "年"
                + ((now.get(Calendar.MONTH)) + 1) + "月第" + wom + "周星期天的日期是："
                + df.format(now.getTime()));
//        model.setEndDateOfWeek(now.getTime());
        model.put("endDateOfWeek", now.getTime());

        return model;
    }
}
